********************************************************************************
** 	TITLE: b10_cfs_cleaning.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Correctional Facility Staff Survey Cleaning for Analysis
**	
**  INPUT: Raw Qualtrics Data for " Correctional Facility Staff Survey"
** 
**  OUTPUT: "$output_data/IGNITE_corrections_survey_clean.dta"	

********************************************************************************
********************************************************************************
*Download Data
********************************************************************************
		set sortseed 13

		capture import excel "$input_data/correctional_facility_survey/Correctional Facility Staff Survey_February 8, 2024_17.21.xlsx", sheet("Sheet0") firstrow case(lower) clear


	*Drop rows that aren't responses
	drop if strpos(startdate, "2024") == 0

********************************************************************************
*Create Labels for Encoded variables
********************************************************************************

*Creating Labels for variables	
	local set1 scope first_survey work_preignite ff_jail respect  work_5yr work_satisfy
	foreach set of local set1{	
	gen d_`set' = 0 if `set' != ""
	replace d_`set' = 1 if strpos(`set', "Yes") > 0	
	}
	
	//consent
	gen d_consent = 0 if consent != ""
	replace d_consent = 1 if strpos(consent, "Continue") > 0	
	
	
	//sex
	gen d_male = 0 if sex != ""
	replace d_male = 1 if strpos(sex, "Male") > 0
	label var d_male "Male"
	
	//ethnicity
	gen d_ethnicity = 0 if ethnicity != ""
	replace d_ethnicity = 1 if strpos(ethnicity, "Hispanic or Latino") > 0 & strpos(ethnicity, "Not") == 0
	gen d_hispanic = d_ethnicity
	label var d_hispanic "Hispanic"
	*Race
	gen d_black = .
	replace d_black = 0 if race != ""
	replace d_black = 1 if strpos(race, "Black") > 0
	label var d_black "Black"
	
	gen d_white = .
	replace d_white = 0 if race != ""
	replace d_white = 1 if strpos(race, "White") > 0
	label var d_white "White"

	gen d_other_race = .
	replace d_other_race = 0 if race != ""
	replace d_other_race = 1 if strpos(race, "Other") > 0
	label var d_other_race "Other Race"

		*Age
	gen age_grp_t = 0 if age_grp != ""

	forval t =1(1)6{
				
		if `t'==1{
			local type "18 - 24"
		}
		if `t'==2{
			local type "25 - 34"
		}
		if `t'==3{
			local type "35 - 44"
		}
					
		if `t'==4{
			local type "45 - 54"
		}
		if `t'==5{
			local type "55 - 64"
		}
		if `t'==6{
			local type "65+"
		}
	gen d_age_`t' = 0 if age_grp != ""
	replace d_age_`t' = 1 if strpos(age_grp, "`type'") > 0
	label var d_age_`t' "Age `type'"
	
replace age_grp_t = `t' if strpos(age_grp, "`type'") > 0
	}

	
		*Work Length
	gen work_length_t = 0 if work_length != ""

	forval t =1(1)5{
				
		if `t'==1{
			local type "Less than 1 year"
		}
		if `t'==2{
			local type "1-2 years"
		}
		if `t'==3{
			local type "3-4 years"
		}
					
		if `t'==4{
			local type "5-6 years"
		}
		if `t'==5{
			local type "7+ years"
		}
	gen d_work_length`t' = 0 if work_length != ""
	replace d_work_length`t' = 1 if strpos(work_length, "`type'") > 0
	label var d_work_length`t' "Work `type'"
	
	replace work_length_t = `t' if strpos(work_length, "`type'") > 0
	}
	
	//post-covid
	gen d_postcovid = 0 if work_length != "" 
	replace d_postcovid = 1 if d_work_length4 == 0 & d_work_length5 == 0
	label var d_postcovid "Started work after Covid"

	//Regular contact
	gen d_contact = 0 if inmate_contact != "" 
	replace d_contact = 1 if strpos(inmate_contact, "Usually") > 0 | strpos(inmate_contact, "Always") > 0 
	label var d_contact "Regular Contact with Inmates"
	gen contact_t = . if inmate_contact != ""
replace contact_t = 1 if strpos(inmate_contact, "Never") > 0
replace contact_t = 2 if strpos(inmate_contact,  "Seldom") > 0 
replace contact_t = 3 if strpos(inmate_contact, "About half the time") > 0
replace contact_t = 4 if strpos(inmate_contact, "Usually") > 0 
replace contact_t = 5 if strpos(inmate_contact, "Always") > 0
	label var contact_t "Contact with Inmates"

	*Education	
	gen edu_t = 0 if edu != ""
	forval t =1(1)4{
				
		if `t'==1{
			local type "No high school degree"
		}
		if `t'==2{
			local type "High school degree or GED"
		}
		if `t'==3{
			local type "Some College"
		}
					
		if `t'==4{
			local type "Completed college or more."
		}
	gen d_edu`t' = 0 if edu != ""
	replace d_edu`t' = 1 if strpos(edu, "`type'") > 0
	label var d_edu`t' "Education `type'"
	replace edu_t = `t' if strpos(edu, "`type'") > 0
	label var edu_t "Educational Attainment"
	}

	
	*Division	
	forval t =1(1)4{
				
		if `t'==1{
			local type "Corrections"
		}
		if `t'==2{
			local type "Courts"
		}
		if `t'==3{
			local type "Road Patrol"
		}
					
		if `t'==4{
			local type "Other"
		}
	gen d_division`t' = 0 if division != ""
	replace d_division`t' = 1 if strpos(division, "`type'") > 0
	label var d_division`t' "Division `type'"
	}

	*Role	
	forval t =1(1)5{
				
		if `t'==1{
			local type "Detention Administrator"
		}
		if `t'==2{
			local type "Detention Command Staff"
		}
		if `t'==3{
			local type "Detention First-Line Supervisor"
		}
					
		if `t'==4{
			local type "Detention Officer"
		}
		if `t'==5{
			local type "Other"
		}
	gen d_role`t' = 0 if role != ""
	replace d_role`t'  = 1 if strpos(role, "`type'") > 0
	label var d_role`t' "Role `type'"
	}


//Qualitative Sentiment Ratings from 1 (Strongly Disagree) to 5 (Strongly Agree)
	
local set12 work_inmates_1 work_inmates_2 work_inmates_3 work_inmates_4 ignite_culture_1 ignite_culture_2 ignite_prepost_1 ignite_prepost_2 ignite_prepost_3 work_experience_1 work_experience_2 work_experience_3
label define agree_disd 1 "Strongly Disagree" 2 "Disagree" 3 "Neither Agree nor Disagree" 4 "Agree" 5 "Strongly Agree"
foreach set of local set12{	
gen `set'_t = 0 if `set' != ""
replace `set'_t = 1 if strpos(`set', "Strongly Disagree") > 0
replace `set'_t = 2 if strpos(`set', "Disagree") > 0 & strpos(`set', "Strongly") == 0 & strpos(`set', "Neither") == 0
replace `set'_t = 3 if strpos(`set', "Neither Agree nor Disagree") > 0
replace `set'_t = 4 if strpos(`set', "Agree") > 0 & strpos(`set', "Strongly") == 0 & strpos(`set', "Neither") == 0
replace `set'_t = 5 if strpos(`set', "Strongly Agree") > 0

gen d_`set' = 0 if `set'_t != .
replace d_`set' = 1 if `set'_t >3 & `set'_t != .

gen d_`set'_neu = 0 if `set'_t != .
replace d_`set'_neu = 1 if `set'_t == 3

gen d_`set'_neg = 0 if `set'_t != .
replace d_`set'_neg = 1 if `set'_t <3 & `set'_t != .

******Creating dummies for each sentiment for an outcome
	forval t =1(1)5{
				
		if `t'==1{
			local type  "Strongly Disagree"
		}
		if `t'==2{
			local type "Disagree"
		}
		if `t'==3{
			local type "Neither Agree nor Disagree"
		}
					
		if `t'==4{
			local type "Agree"
		}
		if `t'==5{
			local type "Strongly Agree"
		}
	gen d_`set'`t' = 0 if `set'_t != .
	replace d_`set'`t'  = 1 if `set'_t == `t'
	label var d_`set'`t' "`set': `type'"
	}
}

gen similar_inmate_t = 0 if similar_inmate != ""
replace similar_inmate_t = 1 if strpos(similar_inmate, "Very different") > 0
replace similar_inmate_t = 2 if strpos(similar_inmate, "Somewhat different") > 0 
replace similar_inmate_t = 3 if strpos(similar_inmate, "About the same") > 0
replace similar_inmate_t = 4 if strpos(similar_inmate, "Somewhat similar") > 0
replace similar_inmate_t = 5 if strpos(similar_inmate, "Very similar") > 0
gen d_similar_inmate = 0 if similar_inmate_t <=3
replace d_similar_inmate = 1 if similar_inmate_t >3 & similar_inmate_t != .

gen d_similar_inmate_neu = 0 if similar_inmate_t != .
replace d_similar_inmate_neu = 1 if similar_inmate_t ==3

gen d_similar_inmate_neg = 0 if similar_inmate_t != .
replace d_similar_inmate_neg = 1 if similar_inmate_t < 3  & similar_inmate_t != .


******Creating dummies for each sentiment for similar_inmate_t
	forval t =1(1)5{
				
		if `t'==1{
			local type  "Strongly Disagree"
		}
		if `t'==2{
			local type "Disagree"
		}
		if `t'==3{
			local type "Neither Agree nor Disagree"
		}
					
		if `t'==4{
			local type "Agree"
		}
		if `t'==5{
			local type "Strongly Agree"
		}
	gen d_similar_inmate`t' = 0 if similar_inmate_t != .
	replace d_similar_inmate`t' = 1 if similar_inmate_t == `t'
	label var d_similar_inmate`t' "Staff/Inmates:`type'"
	}


********************************************************************************
*Create date survey taken variable
********************************************************************************	
	
	*Medium used to complete survey	
	forval t =1(1)3{
				
		if `t'==1{
			local type  "Phone"
		}
		if `t'==2{
			local type "Computer"
		}
		if `t'==3{
			local type "Tablet"
		}			
	gen d_tool_complete`t' = 0 if tool_complete != ""
	replace d_tool_complete`t' = 1 if strpos(tool_complete, "`type'") > 0
	label var d_tool_complete`t' "Tool `type'"
	}


gen d_surveycompleted = 0
replace d_surveycompleted = 1 if tool_complete != ""
label var d_surveycompleted "Completed Survey"

gen d_surveyforward = 0
replace d_surveyforward = 1 if d_consent == 1 & d_scope == 1 
label var d_surveyforward "Continued with Survey"

	
	split recordeddate, gen(date)
	gen date = date(date1, "MDY")
	format date %td
	rename date2 time
	drop date1
	gen sdate = string(date, "%td")

	labmask date, values(sdate) //used to output dates in figures/tables
	gen survey_month = mofd(date)
	format survey_month %tm
	

********************************************************************************
*F. Label Variables
********************************************************************************	
	
	label var d_consent "Consent"
	label var d_scope "Agree to Scope"
	label var d_first_survey "First Survey"
	label var work_length_t "Work Length"
	label var d_work_preignite "worked before IGNITE"
	label var inmate_contact "How often do you interact with inmates in IGNITE?"
	label var age_grp_t "Age"
	label var sex "Sex at birth?"
	label var ethnicity "What is your ethnicity?"
	label var race "What is your race? - Selected Choice"
	label var edu "What is your highest level of education completed?"
	label var division "What division did/do you work in at the facility? [check all that apply]"
	label var role "What is your role within the facility?"
	label var d_ff_jail "Friend or family ever been in jail?"

//Work_inmates
	label var work_inmates_1_t "Rehabilitation programs in jail are worth the time and money. (5 pt. scale)"
*programworth
	
	label var work_inmates_2_t "Education programs in jail are worth the time and money. (5 pt. scale)"
*educationworth

	label var work_inmates_3_t "Rehabilitating just as important as Punishment. (5 pt. scale)"
*rehabimport
	
	label var work_inmates_4_t "Important that law enforcement have good community relations. (5 pt. scale)"
*lepositiv
	
	label var similar_inmate_t "Similarity between officers and inmates? (5 pt. scale)"
	
	label var d_respect "Mutual respect between inmates and staff?"

	label var respect_yes "Mutual respect between inmates and staff: Yes"
	
	label var respect_no "Mutual respect between inmates and staff: No"
	
	label var ignite_inmate "Difference between IGNITE and Non-IGNITE participants?"

//ignite_culture
    
	label var ignite_culture_1_t "Better experience with IGNITE inmates (5 pt. scale)"
*igniteinmateexper
	label var ignite_culture_2_t "Higher morale among IGNITE inmates (5 pt. scale)"
*igniteinmatemoral

	
//add qs pre post
	label var ignite_prepost_1_t "IGNITE has improved my working experience (5 pt. scale)"
*workexpignite

	label var ignite_prepost_2_t "IGNITE has improved employee morale. (5 pt. scale)"
*workmoraleignite

	label var ignite_prepost_3_t "IGNITE has improved jailed individuals' morale. (5 pt. scale)"
*inmatemoraleignite
	label var ignite_prepost_resp "Ways in which IGNITE has changed working Experience"


	label var  d_work_5yr "Want to continue working at the Jail in five years?"

//work experience
  
	label var work_experience_1_t "Satisfied with my job. (5 pt. scale)"
*jobsatisfy

	label var work_experience_2_t "My benefits package is competitive. (5 pt. scale)"
*jobpackagecompete

	label var work_experience_3_t "Pay is high enough. (5 pt. scale)"
*jobpayhigh

	label var d_work_satisfy "Satisfied with the management (e.g., compensation, benefits)?"

	label var tool_complete "What did you use to complete this survey?"

	label var add_info "Anything else to add?"

	label var giftcard_url "Received Giftcard"

	
local set12 work_inmates_1 work_inmates_2 work_inmates_3 work_inmates_4 ignite_culture_1 ignite_culture_2 ignite_prepost_1 ignite_prepost_2 ignite_prepost_3 work_experience_1 work_experience_2 work_experience_3 similar_inmate
foreach set of local set12{
lab var d_`set' "`: var lab `set'_t' Agree"	
}


drop startdate-userlanguage d_ethnicity giftcard_url createnewfieldorchoosefrom
gen id = _n
keep id race_3_text division_4_text role_5_text respect_yes respect_no ignite_inmate ignite_prepost_resp add_info d_scope-survey_month
order id d_scope-survey_month, first

	saveold "$output_data/IGNITE_corrections_survey_clean.dta", replace
